package com.basic.library.view.recyclerview;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Typeface;
import android.util.AttributeSet;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import androidx.recyclerview.R;
import com.basic.library.brvah.MBaseViewHolder;
import com.basic.library.brvah.MyBaseMultiItemQuickAdapter;
import com.basic.library.brvah.MyBaseQuickAdapter;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.entity.MultiItemEntity;

/**
 * @author yj
 * @description 页脚RecyclerViewW
 */
public class RecyclerViewWithPage extends RecyclerView {

    public RecyclerViewWithPage(@NonNull Context context) {
        this(context, null);
    }

    public RecyclerViewWithPage(Context context, AttributeSet attrs) {
        this(context, attrs, R.attr.recyclerViewStyle);
    }

    public RecyclerViewWithPage(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    int radius = 60;
    int leftMargin = 50;
    int bottomMargin = 80;

    @Override
    public void draw(Canvas c) {
        super.draw(c);
        getText(c);
    }

    Paint paint, paintStroke, paint_line;

    void init() {
        setWillNotDraw(false);
        paint = new Paint();                       //绘制白色的圆
        paint.setColor(Color.WHITE);
        paint.setAntiAlias(true);
        paint.setStyle(Paint.Style.FILL);

        paintStroke = new Paint();            //绘制灰色的环
        paintStroke.setColor(Color.parseColor("#E4E4E4"));
        paintStroke.setAntiAlias(true);
        paintStroke.setStyle(Paint.Style.STROKE);
        paintStroke.setStrokeWidth(4);

        paint_line = new Paint();                       //绘制直线
        paint_line.setColor(Color.BLACK);
        paint_line.setAntiAlias(true);
        paint_line.setStrokeWidth(3);
    }

    int curIndex, topIndex;

    void getText(Canvas canvas) {
        String topText;
        String bottomText;
        int total = 0;
        BaseQuickAdapter adapter = (BaseQuickAdapter) getAdapter();
        if (adapter == null) return;
        LinearLayoutManager linearManager = (LinearLayoutManager) getLayoutManager();
        int topIndex = (linearManager.findLastVisibleItemPosition() - adapter.getHeaderLayoutCount()) + 1; //最终是要+1
        int curIndex = topIndex;
        if (getAdapter() instanceof MyBaseQuickAdapter) {
            MyBaseQuickAdapter adapter1 = ((MyBaseQuickAdapter) adapter);
            total = adapter1.totalCount;
            if (adapter1.totalCount == 0) {
                for (int i = 0; i < adapter1.getData().size(); i++) {
                    total++;
                }
            }
            this.topIndex = topIndex;
        } else if (getAdapter() instanceof MyBaseMultiItemQuickAdapter) {
            MyBaseMultiItemQuickAdapter<MultiItemEntity, MBaseViewHolder> adapter1 = (MyBaseMultiItemQuickAdapter) adapter;
            if (this.curIndex != curIndex) { //2次可见item位置不一样时计算，省去不必要的计算
                this.curIndex = curIndex;
                if (adapter1.getData().size() != 0) {
                    int size = topIndex;
                    for (int i = 0; i < size; i++) {
                        if (i < adapter1.getData().size())
                            if (adapter1.getData().get(i).getItemType() == 0) {
                                topIndex--;
                            }
                    }
                }
                this.topIndex = topIndex;
            } else {
                topIndex = this.topIndex;
            }
            total = adapter1.totalCount;
        }
        if (topIndex > adapter.getData().size()) {
            topIndex = adapter.getData().size();
        }
        if (topIndex > total)
            topIndex = total;
        topText = topIndex + "";

        if (total < 1) {
            return;
        }
        bottomText = total + "";

        float centerX = getWidth() - radius - leftMargin;
        float centerY = getHeight() - radius - bottomMargin;
        canvas.drawCircle(centerX, centerY, radius, paint);

        canvas.drawCircle(centerX, centerY, radius + 2, paintStroke);

        float startX = getWidth() - radius * 2 - leftMargin + 5;
        float startY = getHeight() - radius - bottomMargin;
        float stopX = getWidth() - leftMargin - 5;
        float stopY = startY;
        canvas.drawLine(startX, startY, stopX, stopY, paint_line);

        drawText(canvas, false, topText, bottomText);
        drawText(canvas, true, topText, bottomText);
    }


    /**
     * //绘制字符串
     *
     * @param canvas
     */
    void drawText(Canvas canvas, boolean top, String topText, String bottomText) {
        int left = getWidth() - radius * 2 - leftMargin + 5;
        int startY = getHeight() - radius - bottomMargin;
        int stopX = getWidth() - leftMargin - 5;
        Rect targetRect = new Rect(left, startY + 2, stopX, startY + 42); //bottom
        if (top)
            targetRect = new Rect(left, startY - 42, stopX, startY - 2);
        Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setTextSize(25);
        paint.setTypeface(Typeface.DEFAULT_BOLD);
        paint.setColor(Color.TRANSPARENT);
        canvas.drawRect(targetRect, paint);
        paint.setColor(Color.parseColor("#3D3D3D"));
        Paint.FontMetricsInt fontMetrics = paint.getFontMetricsInt();
        int baseline = (targetRect.bottom + targetRect.top - fontMetrics.bottom - fontMetrics.top) / 2;
        // 下面这行是实现水平居中，drawText对应改为传入targetRect.centerX()
        paint.setTextAlign(Paint.Align.CENTER);
        if (top)
            canvas.drawText(topText, targetRect.centerX(), baseline, paint);
        else
            canvas.drawText(bottomText, targetRect.centerX(), baseline, paint);
    }
}
